//
//  PicCollectionView.swift
//  WB
//
//  Created by leador on 17/5/10.
//  Copyright © 2017年 袁满. All rights reserved.
//

import UIKit

class PicCollectionView: UICollectionView {
    
    var picURLs:[URL] = [URL]() {
        
        didSet {
            
            self.reloadData()
            
        }
        
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        dataSource = self
        delegate = self
    }
    
}

extension PicCollectionView:UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return picURLs.count
        
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCellID", for: indexPath) as! PicCollectionViewCell
    
        cell.picURL = picURLs[indexPath.item]
        
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let userInfo = [showPhotoBrowerIndexPathKey:indexPath,showPhotoBrowerUrlsKey:picURLs] as [String : Any]
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: showPhotoBrowerNotification), object: nil, userInfo: userInfo)
        
    }
    
}

class PicCollectionViewCell: UICollectionViewCell {
    
    var picURL : URL? {
    
        didSet {
            guard let picURL = picURL else {
                return
            }
            
            iconView.sd_setImage(with:picURL,placeholderImage:#imageLiteral(resourceName: "empty_picture"))
        }
        
    }

    @IBOutlet weak var iconView: UIImageView!

}



